package org.anddev.andengine.extension.svg.node;
/**
 * @author Eric Depta
 * @since 01:14:00 - 28.02.2011
 */
import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.extension.svg.SVGDoc;
import org.anddev.andengine.extension.svg.SVGPath;
import org.anddev.andengine.extension.svg.util.math.Ellipse;
import org.anddev.andengine.extension.svg.util.physic.ExtendedPhysicsFactory;
import org.anddev.andengine.extension.svg.util.transform.TransformDoc;
import org.anddev.andengine.util.SAXUtils;
import org.xml.sax.Attributes;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

//-- Extends Path cause scaleX!=scaleY make ellipse
public class CircleNode extends PathNode {

	public CircleNode(final SVGDoc pSVGDoc){
		super(pSVGDoc);
	}
	
	public void create(final Attributes pAttributes, final TransformDoc pTransform){
		super.create(pAttributes, pTransform);
	}
	
	@Override
	protected void createPath(final Attributes pAttributes){
		final List<Vector2> veclist = new ArrayList<Vector2>();
		final int segments = SAXUtils.getIntAttribute(pAttributes, TAG_ATTRIBUTE_CURVE_SEGMENTS, 4);
		Ellipse.ellipse(
			veclist, 
			segments, 
			new Vector2(
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_CENTER_X, 0),
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_CENTER_Y, 0)
			), 
			SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_RADIUS, 0), 
			SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_RADIUS, 0), 
			0, 
			0, 
			360
		);
		this.svgPath = new SVGPath(
			veclist,
			true
		);
	}
	
	@Override
	protected Shape createShape(final String pName, final Attributes pAttributes){
		if(!this.isCircle()){
			return super.createShape(pName, pAttributes);
		}else{
			final String source = SAXUtils.getAttribute(pAttributes, TAG_ATTRIBUTE_SPRITESRC, "");
			Shape shape = null;
			if(source != "" ){
				//-- Sprite
				final float r = SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_RADIUS, 0);
				shape = super.createSprite(
					pName, 
					pAttributes, 
					source,
					SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_CENTER_X, 0) - r, 
					SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_CENTER_Y, 0) - r, 
					r * 2, 
					r * 2,
					0
				);
				super.setHasSprite(true);
			}else if(super.isHasPhysic()){
				//-- Boundings
				shape = new Rectangle(
					SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_X, 0), 
					SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_Y, 0), 
					SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_WIDTH, 0), 
					SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_HEIGHT, 0)
				);
			}
			
			//-- Transform
			super.mTransformation.transformShape(shape);
			
			//-- Primitives
			if(super.isDrawPrimitives()){
				this.createPath(pAttributes);
			}
			super.drawPrimitives(pName, pAttributes, shape);
			
			return shape;
		}
	}
	
	@Override
	protected void createPhysics(final Shape pShape, final String pName, final Attributes pAttributes){
		if(!this.isCircle()){
			super.createPhysics(pShape, pName, pAttributes);
		}else{
			if(super.isHasPhysic()){
				final Body body = ExtendedPhysicsFactory.createCircleBody(
					super.mSVGDoc.mPhysicsWorld, 
					pShape,
					BodyType.values()[SAXUtils.getIntAttribute(pAttributes, TAG_ATTRIBUTE_BODYTYPE, 0)], 
					super.createFixtureDef(pAttributes)
				);
				super.setBodyOptions(pAttributes, body);
				super.registerPhysicsConnector(pAttributes, pShape, body);
				super.mSVGDoc.addBody(pName, body);
			}else if(super.isHasSprite()){
				//-- Velocitys to shape
				super.createPhysicHandler(pAttributes, pShape);
			}
		}
	}
	
	private boolean isCircle(){
		return super.mTransformation.getScaleX() == super.mTransformation.getScaleY();
	}
}
